var base = require('/base_info.js');
var utils = require('/utils/index.js');
Page({
  data () {
    return {
      navIndex: 0,
      discountList: [],
      page: 1,
      size: 10
    };
  },
  // 点击去使用
  home () {
    my.redirectTo({
      url: './../room/room-list'
    });
  },
  // 获取用户优惠券列表
  getData () {
    let that = this;
    utils.getData('SingleMyCoupons', {
      page: that.data.page,
      size: that.data.size,
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data,
      discount_type: that.data.navIndex
    }, (res) => {
      my.hideLoading({
        page: that  // 防止执行时已经切换到其它页面，page指向不准确
      });
      that.setData({
        discountList: that.data.discountList.concat(res.data.data),
        page: that.data.page + 1
      });
    }, (err) => {
      my.hideLoading({
        page: that  // 防止执行时已经切换到其它页面，page指向不准确
      });
      console.log(err);
    });
  },
  // 切换tab
  changeTab (e) {
    this.setData({
      navIndex: e.currentTarget.dataset.id,
      discountList: [],
      page: 1
    });
    my.showLoading();
    this.getData();
  },
  onLoad(query) {
    this.setData({
      navIndex: query.discount_type ? query.discount_type : 0
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    my.showLoading();
    this.setData({
      discountList: [],
      page: 1
    });
    this.getData();
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
    this.getData();
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
